package com.toolbox;


import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
public class MergeFile {

    public static final String SOURCE_PATH = "D:\\需求文档\\CSP接入歌斐PE存续投后数据\\导出\\";

    public static final String FILE_NAME = "导出内容";

    public static final String END_NAME = ".sql";

    public static final String MAIN_SOURCE_FILE_URL = "D:\\需求文档\\CSP接入歌斐PE存续投后数据\\导出\\导出内容.sql";

    public static final String CHECK_META_DATA_FILE_URL = "D:\\需求文档\\CSP接入歌斐PE存续投后数据\\导出\\抽查报告数据.txt";

    public static final String FORMART_STR = "${condition}";

    public static void main(String[] args) throws IOException {

        Path mainDataPath = Paths.get(MAIN_SOURCE_FILE_URL);

        List<String> mainSourceList = Files.lines(mainDataPath).collect(Collectors.toList());

        //todo 取数据拆出来做
        Path checkMetaDataPath = Paths.get(CHECK_META_DATA_FILE_URL);
        List<String> checkMetaDatas = Files.lines(checkMetaDataPath).collect(Collectors.toList());

        //todo 原数据格式化拆开做
        Map<LocalDate, List<ReportDto>> dataMap = convertList(checkMetaDatas).stream().collect(Collectors.groupingBy(i -> i.getReportDate()));


        dataMap.forEach((k, v) -> {
            String condition = v.stream().map(i -> i.convertResultStr()).collect(Collectors.joining(" or "));
            List<String> finalDatas = mainSourceList.stream().map(i -> {
                if (i.contains(FORMART_STR)) {
                    return i.replace(FORMART_STR, condition);
                }
                return i;
            }).collect(Collectors.toList());

            //todo 创建新文件

            try {
                Path path = Paths.get(SOURCE_PATH + FILE_NAME + k.toString() + END_NAME);
                if (Files.exists(path)) {
                    Files.delete(path);
                    log.info("delete file name={}",path.toString());
                }
                Files.write(path, finalDatas);
                log.info("write file name={}",path.toString());
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }

    private static List<ReportDto> convertList(List<String> checkMetaDatas) {
        return checkMetaDatas.stream().map(i -> {
            String[] split = i.split(",");

            ReportDto reportDto = new ReportDto();
            reportDto.setFundCode(split[0]);
            reportDto.setReportDate(LocalDate.parse(split[1]));
            return reportDto;
        }).collect(Collectors.toList());
    }

    @Data
    private static class ReportDto {

        private String fundCode;

        private LocalDate reportDate;

        public String getFundCode() {
            return fundCode;
        }

        public void setFundCode(String fundCode) {
            this.fundCode = fundCode;
        }

        public LocalDate getReportDate() {
            return reportDate;
        }

        public void setReportDate(LocalDate reportDate) {
            this.reportDate = reportDate;
        }


        public String convertResultStr() {
            return "(mf.fund_code='" + fundCode + "' and mf.confirm_date='" + reportDate + "')";
        }
    }
}
